/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WebFEApplet.java
 *
 * Created on Dec 25, 2008, 12:11:49 PM
 */
package com.shailan.projects.webfea.gui;

import com.shailan.projects.webfea.*;
import com.shailan.projects.webfea.grid.Grid;
import com.shailan.util.fem.node.Node;
import com.shailan.util.fem.element.Truss_3D;
import com.shailan.util.fem.loader.S2KLoader;
import com.shailan.util.fem.model.Model3DFrame;
import com.shailan.util.fem.model.Model3DTruss;
import com.sun.opengl.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.Enumeration;
import java.util.StringTokenizer;
import javax.swing.JApplet;

public class WebFEApplet extends JApplet {

    private Animator animator;
    private GLRenderer glRenderer;
    private Model3DFrame model;
    private Grid grid = new Grid();
    private ModelViewer viewer = new ModelViewer();
    private String filePath = "models/truss.s2k";

    /** Initializes the applet WebFEApplet */
    public void init() {

        filePath=getParameter("model");
        model = new Model3DFrame();
        try {
            //loadModel(filePath);
            loadS2KModel(filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Total nodes:"+model.getNodesNum());
        System.out.println("Total frames:"+model.getFramesNum());
        viewer.setGrid(grid);
        viewer.setModel(model);
        glRenderer = new GLRenderer();
        glRenderer.setViewer(viewer);

        initComponents();
        gLCanvas1.addGLEventListener(glRenderer);
        animator = new FPSAnimator(gLCanvas1, 60);


    }

    public void start() {
        animator.start();

    }

    public void stop() {
        // FIXME: do I need to do anything else here?
        animator.stop();

    }


    public void loadS2KModel(String fileName) throws FileNotFoundException, IOException, ParseException{
        String file_src, line_st;

        String line;
        URL url = null;
        try {
            url = new URL(getCodeBase(), fileName);
        } catch (MalformedURLException e) {
        }

        InputStream in = url.openStream();
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        StringBuffer strBuff = new StringBuffer();
        while ((line = bf.readLine()) != null) {
            strBuff.append(line + "\n");
        }

        // Start parsing
        file_src = strBuff.toString();

        S2KLoader sload=new S2KLoader(model,file_src);
        sload.loadModel();
    }

    private void loadModel(String fileName) throws FileNotFoundException, IOException, ParseException {
        String file_src, line_st;

        String line;
        URL url = null;
        try {
            url = new URL(getCodeBase(), fileName);
        } catch (MalformedURLException e) {
        }

        InputStream in = url.openStream();
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        StringBuffer strBuff = new StringBuffer();
        while ((line = bf.readLine()) != null) {
            strBuff.append(line + "\n");
        }

        // Start parsing
        file_src = strBuff.toString();

        // NODES
        StringTokenizer stt = new StringTokenizer(file_src, "\n");
        while (stt.hasMoreTokens()) {
            line_st = stt.nextToken().trim().toUpperCase();
            if (line_st.startsWith("NODES")) {
                while (stt.hasMoreTokens()) {
                    line_st = stt.nextToken().trim().toUpperCase();
                    if (line_st.startsWith("#") || line_st.length() == 0) {
                        continue;
                    }
                    if (line_st.startsWith("ELEMENTS")) {
                        break;
                    }
                    StringTokenizer stt1 = new StringTokenizer(line_st, "\t");
                    String key = stt1.nextToken();
                    //model.addNode(Float.parseFloat(stt1.nextToken()), Float.parseFloat(stt1.nextToken()), Float.parseFloat(stt1.nextToken()));
                }
            }
        }

        // ELEMENTS
        stt = new StringTokenizer(file_src, "\n");
        while (stt.hasMoreTokens()) {
            line_st = stt.nextToken().trim().toUpperCase();
            if (line_st.startsWith("ELEMENTS")) {
                while (stt.hasMoreTokens()) {
                    line_st = stt.nextToken().trim().toUpperCase();
                    if (line_st.startsWith("#") || line_st.length() == 0) {
                        continue;
                    }
                    if (line_st.startsWith("MATERIAL")) {
                        break;
                    }
                    StringTokenizer stt1 = new StringTokenizer(line_st, "\t");
                    String key = stt1.nextToken();
                    //Truss_3D elem = new Truss_3D(1.0f, 1.0f, model.getNode(Integer.parseInt(stt1.nextToken())), model.getNode(Integer.parseInt(stt1.nextToken())));
                    //model.addElement(elem);
                }
            }
        }
    }

    public void saveModel() {/*
        try {
            File f = new File(filePath);
            String text="";
            if (f.exists()) {
                BufferedWriter out = new BufferedWriter(new FileWriter(f, true)); 
                //WRITE NODES
                text+="NODES\n";
                //Enumeration nods = model.getNodes().elements();
                int i=0;
                while (nods.hasMoreElements()) {
                    Node n = (Node) nods.nextElement();
                    text+=i+"\t"+n.getX()+"\t"+n.getY()+"\t"+n.getZ()+"\n";
                    i++;
                }

                //WRITE ELEMENTS
                text+="ELEMENTS\n";
                Enumeration elems = model.getElements().elements();
                i=0;
                while (elems.hasMoreElements()) {
                    Truss_3D elem = (Truss_3D) elems.nextElement();
                    Node nI = elem.getINode();
                    Node nJ = elem.getJNode();
                    text+=i+"\t"+nI.getId()+"\t"+nJ.getId()+"\n";
                }
                out.write(text);
                if(f.canWrite()){
                    System.out.println("Model dosyasi yazildi.");
                }else{
                    System.out.println("Model dosyasi yazilamadi.");
                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
                              **/
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileButtonGroup = new javax.swing.ButtonGroup();
        drawButtonGroup = new javax.swing.ButtonGroup();
        viewButtonGroup = new javax.swing.ButtonGroup();
        mouseModeButtonGroup = new javax.swing.ButtonGroup();
        addNodeDialog = new javax.swing.JDialog();
        jLabel2 = new javax.swing.JLabel();
        addNode_X = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        addNode_Y = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        addNode_Z = new javax.swing.JTextField();
        addNodeButton2 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        addElementDialog = new javax.swing.JDialog();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        commandLine = new javax.swing.JEditorPane();
        jToolBar1 = new javax.swing.JToolBar();
        newButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        addNodeButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        zoomInButton = new javax.swing.JButton();
        zoomOutButton = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        gridToggleButton = new javax.swing.JToggleButton();
        rotateModeButton = new javax.swing.JToggleButton();
        moveModeButton = new javax.swing.JToggleButton();
        scaleModeButton = new javax.swing.JToggleButton();
        gLCanvas1 = new javax.media.opengl.GLCanvas();
        jMenuBar1 = new javax.swing.JMenuBar();
        Menu_file = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        Menu_edit = new javax.swing.JMenu();
        Menu_preProcess = new javax.swing.JMenu();
        Menu_process = new javax.swing.JMenu();
        Menu_postProcess = new javax.swing.JMenu();

        jLabel2.setText("X Coord  :");

        addNode_X.setText("0.00");

        jLabel3.setText("Y Coord  :");

        addNode_Y.setText("0.00");

        jLabel4.setText("Z Coord  :");

        addNode_Z.setText("0.00");

        addNodeButton2.setText("Add Node");
        addNodeButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNodeButton2ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");

        javax.swing.GroupLayout addNodeDialogLayout = new javax.swing.GroupLayout(addNodeDialog.getContentPane());
        addNodeDialog.getContentPane().setLayout(addNodeDialogLayout);
        addNodeDialogLayout.setHorizontalGroup(
            addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addNodeDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addNodeDialogLayout.createSequentialGroup()
                        .addComponent(addNodeButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addNodeDialogLayout.createSequentialGroup()
                        .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                        .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addNode_Z)
                            .addComponent(addNode_Y, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(addNode_X, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))))
                .addContainerGap())
        );
        addNodeDialogLayout.setVerticalGroup(
            addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addNodeDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(addNode_X, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(addNode_Y, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(addNode_Z, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(addNodeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(addNodeButton2))
                .addContainerGap())
        );

        jLabel5.setText("iNode");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList1);

        jLabel6.setText("jNode");

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList2);

        jButton1.setText("Add");

        jButton3.setText("jButton3");

        jLabel7.setText("Section");

        jList3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane4.setViewportView(jList3);

        javax.swing.GroupLayout addElementDialogLayout = new javax.swing.GroupLayout(addElementDialog.getContentPane());
        addElementDialog.getContentPane().setLayout(addElementDialogLayout);
        addElementDialogLayout.setHorizontalGroup(
            addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addElementDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(22, 22, 22)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                .addGap(48, 48, 48))
            .addGroup(addElementDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addElementDialogLayout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                    .addGroup(addElementDialogLayout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)))
                .addGap(48, 48, 48))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addElementDialogLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addContainerGap())
        );
        addElementDialogLayout.setVerticalGroup(
            addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addElementDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(addElementDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addGap(24, 24, 24))
        );

        jScrollPane1.setViewportView(commandLine);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/new.gif"))); // NOI18N
        fileButtonGroup.add(newButton);
        newButton.setFocusable(false);
        newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(newButton);

        openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/open.gif"))); // NOI18N
        fileButtonGroup.add(openButton);
        openButton.setFocusable(false);
        openButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        openButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(openButton);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/save.gif"))); // NOI18N
        fileButtonGroup.add(saveButton);
        saveButton.setFocusable(false);
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(saveButton);

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);

        addNodeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/node.png"))); // NOI18N
        drawButtonGroup.add(addNodeButton);
        addNodeButton.setFocusable(false);
        addNodeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addNodeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addNodeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNodeButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(addNodeButton);

        jLabel1.setText("|");
        jToolBar2.add(jLabel1);

        zoomInButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/zoomin.gif"))); // NOI18N
        zoomInButton.setFocusable(false);
        zoomInButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        zoomInButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        zoomInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zoomInButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(zoomInButton);

        zoomOutButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/zoomout.gif"))); // NOI18N
        zoomOutButton.setFocusable(false);
        zoomOutButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        zoomOutButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        zoomOutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zoomOutButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(zoomOutButton);

        viewButtonGroup.add(jToggleButton1);
        jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/view_front.gif"))); // NOI18N
        jToggleButton1.setFocusable(false);
        jToggleButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        jToolBar2.add(jToggleButton1);

        viewButtonGroup.add(jToggleButton2);
        jToggleButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/view_back.gif"))); // NOI18N
        jToggleButton2.setFocusable(false);
        jToggleButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });
        jToolBar2.add(jToggleButton2);

        viewButtonGroup.add(jToggleButton3);
        jToggleButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/view_top.gif"))); // NOI18N
        jToggleButton3.setFocusable(false);
        jToggleButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton3ActionPerformed(evt);
            }
        });
        jToolBar2.add(jToggleButton3);

        viewButtonGroup.add(jToggleButton4);
        jToggleButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/view_left.gif"))); // NOI18N
        jToggleButton4.setFocusable(false);
        jToggleButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton4ActionPerformed(evt);
            }
        });
        jToolBar2.add(jToggleButton4);

        gridToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/grid.gif"))); // NOI18N
        gridToggleButton.setSelected(true);
        gridToggleButton.setFocusable(false);
        gridToggleButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gridToggleButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        gridToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gridToggleButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(gridToggleButton);

        mouseModeButtonGroup.add(rotateModeButton);
        rotateModeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/rotate.gif"))); // NOI18N
        rotateModeButton.setSelected(true);
        rotateModeButton.setFocusable(false);
        rotateModeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        rotateModeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        rotateModeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rotateModeButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(rotateModeButton);

        mouseModeButtonGroup.add(moveModeButton);
        moveModeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/move.gif"))); // NOI18N
        moveModeButton.setFocusable(false);
        moveModeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        moveModeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        moveModeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveModeButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(moveModeButton);

        mouseModeButtonGroup.add(scaleModeButton);
        scaleModeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shailan/projects/webfea/gui/scale.gif"))); // NOI18N
        scaleModeButton.setFocusable(false);
        scaleModeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        scaleModeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        scaleModeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scaleModeButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(scaleModeButton);

        Menu_file.setText("File");

        jMenuItem1.setText("Item");
        Menu_file.add(jMenuItem1);

        jMenuBar1.add(Menu_file);

        Menu_edit.setText("Edit");
        jMenuBar1.add(Menu_edit);

        Menu_preProcess.setText("Pre-Processor");
        jMenuBar1.add(Menu_preProcess);

        Menu_process.setText("Processor");
        jMenuBar1.add(Menu_process);

        Menu_postProcess.setText("Post-Processor");
        jMenuBar1.add(Menu_postProcess);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(gLCanvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 807, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gLCanvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
        // TODO add your handling code here:
        glRenderer.setView(0.0f, 180.0f, 0.0f);
    }//GEN-LAST:event_jToggleButton2ActionPerformed

    private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton3ActionPerformed
        // TODO add your handling code here:
        glRenderer.setView(90.0f, 0.0f, 0.0f);
    }//GEN-LAST:event_jToggleButton3ActionPerformed

    private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton4ActionPerformed
        // TODO add your handling code here:
        glRenderer.setView(0.0f, 90.0f, 0.0f);
    }//GEN-LAST:event_jToggleButton4ActionPerformed

    private void addNodeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNodeButtonActionPerformed
        // TODO add your handling code here:
        String text = this.commandLine.getText();
        this.commandLine.setText(text + "Node coordinates (x,y,z):");
        this.commandLine.requestFocus();
        addNodeDialog.setBounds(getWidth() / 2 - 125, getHeight() / 2 - 100, 250, 200);
        addNodeDialog.setResizable(false);
        //addNodeDialog.setSize(200, 150);
        addNodeDialog.setVisible(true);
    //this.model.addNode(0.5f,0.5f ,0.5f );
    }//GEN-LAST:event_addNodeButtonActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        glRenderer.setView(0.0f, 0.0f, 0.0f);
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void zoomInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoomInButtonActionPerformed
        // TODO add your handling code here:
        glRenderer.scale += 0.2f;
    }//GEN-LAST:event_zoomInButtonActionPerformed

    private void zoomOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoomOutButtonActionPerformed
        // TODO add your handling code here:
        glRenderer.scale -= 0.2f;
    }//GEN-LAST:event_zoomOutButtonActionPerformed

    private void gridToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gridToggleButtonActionPerformed
        // TODO add your handling code here:
        if (viewer.toggleGrid) {
            viewer.toggleGrid = false;
        } else {
            viewer.toggleGrid = true;
        }

    }//GEN-LAST:event_gridToggleButtonActionPerformed

    private void scaleModeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scaleModeButtonActionPerformed
        // TODO add your handling code here:
        glRenderer.mouseDragMode = 2;
    }//GEN-LAST:event_scaleModeButtonActionPerformed

    private void rotateModeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotateModeButtonActionPerformed
        // TODO add your handling code here:
        glRenderer.mouseDragMode = 0;
    }//GEN-LAST:event_rotateModeButtonActionPerformed

    private void moveModeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveModeButtonActionPerformed
        // TODO add your handling code here:
        glRenderer.mouseDragMode = 1;
    }//GEN-LAST:event_moveModeButtonActionPerformed

    private void addNodeButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNodeButton2ActionPerformed
        // TODO add your handling code here:
        float x = Float.parseFloat(addNode_X.getText());
        float y = Float.parseFloat(addNode_Y.getText());
        float z = Float.parseFloat(addNode_Z.getText());
        
    }//GEN-LAST:event_addNodeButton2ActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:
        saveModel();
    }//GEN-LAST:event_saveButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Menu_edit;
    private javax.swing.JMenu Menu_file;
    private javax.swing.JMenu Menu_postProcess;
    private javax.swing.JMenu Menu_preProcess;
    private javax.swing.JMenu Menu_process;
    private javax.swing.JDialog addElementDialog;
    private javax.swing.JButton addNodeButton;
    private javax.swing.JButton addNodeButton2;
    private javax.swing.JDialog addNodeDialog;
    private javax.swing.JTextField addNode_X;
    private javax.swing.JTextField addNode_Y;
    private javax.swing.JTextField addNode_Z;
    private javax.swing.JEditorPane commandLine;
    private javax.swing.ButtonGroup drawButtonGroup;
    private javax.swing.ButtonGroup fileButtonGroup;
    private javax.media.opengl.GLCanvas gLCanvas1;
    private javax.swing.JToggleButton gridToggleButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.ButtonGroup mouseModeButtonGroup;
    private javax.swing.JToggleButton moveModeButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton openButton;
    private javax.swing.JToggleButton rotateModeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JToggleButton scaleModeButton;
    private javax.swing.ButtonGroup viewButtonGroup;
    private javax.swing.JButton zoomInButton;
    private javax.swing.JButton zoomOutButton;
    // End of variables declaration//GEN-END:variables
}
